package com.birdboot.http;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Parameter;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class HttpServletRequest {
    private Socket socket;

    private String method;
    private String uri;
    private String protocol;

    private String requestURI;
    private String queryString;
    private Map<String,String> parameters = new HashMap<>();

    private Map<String,String> headers = new HashMap<>();

    public HttpServletRequest(Socket socket) throws IOException, EmptyRequestException {
        this.socket = socket;
        parseRequestLine();
        parseHeaders();
        parseContent();
    }
    private void parseRequestLine() throws IOException, EmptyRequestException {
        String line =  readLine();
        if (line.isEmpty()){
            throw new EmptyRequestException();
        }
        System.out.println("请求行："+line);
        String[] data = line.split("\\s");
        method = data[0];
        uri=data[1];
        parseUri();
        protocol = data[2];
        System.out.println("method:"+method);
        System.out.println("uri"+uri);
        System.out.println("protocol:"+protocol);
    }

    private void parseUri(){
        String[] data = uri.split("\\?");
        requestURI = data[0];
        if (data.length>1){
            queryString = data[1];
            String[] paraArray = queryString.split("&");
            for (String para : paraArray) {
                String[] arr = para.split("=",2);
                parameters.put(arr[0],arr[1]);
            }
        }

        System.out.println("requestURI:"+requestURI);
        System.out.println("queryString:"+queryString);
        System.out.println("parameters:"+parameters);
    }

    private void parseHeaders() throws IOException {
        while (true) {
            String line =  readLine();
            if (line.isEmpty()){
                break;
            }
            System.out.println("消息头："+line);
                /*
                    line="Connection: keep-alive";
                                    V
                    data = line.split(":\\s");   data=["Connection", "keep-alive"]
                 */
            //将消息头按照": "进行拆分并存入到headers中
            String[] data = line.split(":\\s");
            headers.put(data[0],data[1]);
        }
        System.out.println("headers:"+headers);
    }
    //解析消息正文
    private void parseContent(){}

    public String getMethod() {
        return method;
    }

    public String getUri() {
        return uri;
    }

    public String getProtocol() {
        return protocol;
    }

    public String getHeaders(String name) {
        return headers.get(name);
    }

    private String readLine() throws IOException {




        InputStream in = socket.getInputStream();
        int d;//用来接收读取的每一个字节
        StringBuilder builder = new StringBuilder();//保存拼接后的一行字符串
        char cur = 'a',pre = 'a';//cur表示本次读取的字符  pre表示上次读取的字符
        while ((d=in.read())!=-1){
            cur = (char)d;//将本次读取的字符赋值给cur
            if (pre==13&&cur==10){//判断上次是否读取的回车符,本次是否为换行符
                break;//若连续读取了回车+换行就停止读取(一行结束了)
            }
            builder.append(cur);
            pre = cur;;//在读取下一个字符前,将本次读取的字符记作"上次读取的字符"
        }
        return builder.toString().trim();

    }

    public String getRequestURI() {
        return requestURI;
    }

    public String getQueryString() {
        return queryString;
    }

    public String getParameter(String name) {
        return parameters.get(name);
    }
}
